//
//  ShouCangViewController.m
//  BSLCar
//
//  Created by 梁毅 on 15/6/3.
//  Copyright (c) 2015年 lyz. All rights reserved.
//

#import "ShouCangViewController.h"
#import "MyTableViewCell.h"

@interface ShouCangViewController ()
{
    UITableView *mtableView;
}
@end

@implementation ShouCangViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的收藏";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"清空收藏" style:UIBarButtonItemStylePlain target:self action:@selector(qingkong)];
    self.dataArray = [NSMutableArray array];
//    [CollectDataBaseManager openDataBase];
     self.dataArray = [NSMutableArray arrayWithArray: [CollectDataBaseManager selectData]];

    if (self.dataArray.count == 0) {
        [self initAlert];
    }else
    {
        [self initTableView];
    }
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // Do any additional setup after loading the view.
}
- (void)initAlert
{
    UIAlertView *alt = [[UIAlertView alloc]initWithTitle:@"提示" message:@"暂无收藏" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
    [alt show];
}
- (void)initTableView
{
    mtableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    mtableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    mtableView.delegate = self;
    mtableView.dataSource = self;
    [self.view addSubview:mtableView];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        
    }
//    [CollectDataBaseManager openDataBase];
    OnMarketForthModel *model = [[CollectDataBaseManager selectData] objectAtIndex:indexPath.row];
    cell.textLabel.text = model.title;
    cell.textLabel.font =[UIFont systemFontOfSize:13];
    cell.detailTextLabel.text =[NSString stringWithFormat:@"%@/%@", model.dealershortname, model.dealeraddress];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.detailTextLabel.textColor = [UIColor blueColor];


    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%ld",[CollectDataBaseManager selectData].count);
    return [CollectDataBaseManager selectData].count;
}
- (void)qingkong
{
    UIAlertView *alt = [[UIAlertView alloc]initWithTitle:nil message:@"确定要清空收藏吗" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"删除某一个", nil];
    [alt show];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    //开启tableview的编辑样式
    [mtableView setEditing:YES animated:animated];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView setEditing:YES animated:YES];
    return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath
{
    
}
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    //如果当前为编辑模式
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        OnMarketForthModel *model = [[CollectDataBaseManager selectData] objectAtIndex:indexPath.row];
        [CollectDataBaseManager deleteValue:model];
        
        [self.dataArray removeObjectAtIndex:indexPath.row];

        
        [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationRight];
        [mtableView reloadData];
//        [self setEditing:NO animated:YES];
   }
   
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {

        [CollectDataBaseManager deleteValue];
        
            [mtableView reloadData];
        
            mtableView.hidden = YES;

    }
     if (buttonIndex == 2) {
         if (self.editing) {
             
             [self setEditing: NO animated: NO];
         } else {
             
             [self setEditing:YES animated:YES];
         }
     }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
